#coding=utf-8



from bs4 import BeautifulSoup


class HtmlParser(object):
    
    
    def _get_new_urls(self,soup):
        new_urls = set()
        
        links = soup.select('.username a')
        for link in links:
            new_url = link['href']
            
            new_urls.add(new_url)
        return new_urls
    
    def _get_new_data(self, page_url, soup):
        res_data = {}
        
        res_data['url'] = page_url
        
        week_visit = soup.select('.count-list li:nth-of-type(3)')
        res_data['week_visit'] = week_visit[0].get_text()
        
        month_visit = soup.select('.count-list li:nth-of-type(4)')
        res_data['month_visit'] = month_visit[0].get_text()
        
        all_visit = soup.select('.count-list li:nth-of-type(5)')
        res_data['all_visit'] = all_visit[0].get_text()
        
        return res_data
    
    def parse(self,page_url,html_cont):
        if page_url is None or html_cont is None:
            return
        
        soup = BeautifulSoup(html_cont,'html.parser',from_encoding='utf-8')
       
        new_data = self._get_new_data(page_url,soup)
        return new_data  

    
    def parseUrl(self, html_cont):
        if html_cont is None:
            return
        
        soup = BeautifulSoup(html_cont,'html.parser',from_encoding='utf-8')
        new_urls = self._get_new_urls(soup)
        
        return new_urls

    
    def _get_count(self, soup):
        res_data = {}
               
        nums = soup.select('.score-num')
        res_data['fans_count'] = nums[2].get_text()
        res_data['fellow_count'] = nums[3].get_text()
        
        return res_data
    
    
    def parseCount(self,html_cont):
        if html_cont is None:
            return
        
        soup = BeautifulSoup(html_cont,'html.parser',from_encoding='utf-8')
        count_data = self._get_count(soup)
        
        return count_data
    
    
    
    
    



